package com.jshoperxms.controller.mall.backstage.member;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.MemberEnums;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.MemberDeliverAddressT;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.service.MemberDeliverAddressTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 17/2/7.
 * Des:
 */
@Controller
@RequestMapping("/mall/memberdeliveraddress")
public class MemberDeliverAddressTController extends BaseTController implements IBaseCtrl<MemberDeliverAddressT> {

    @Resource
    private MemberDeliverAddressTService memberDeliverAddressTService;

    //数据形式
    private Map<String, Object> responsedata = new HashMap<>();


    @RequestMapping(value="/save",method= RequestMethod.POST)
    @Override
    public ResponseEntity save(@RequestBody MemberDeliverAddressT rq) throws ParseException {
        MemberDeliverAddressT bean=new MemberDeliverAddressT();
        bean.setId(this.serial.Serialid(SerialKeys.MEMBERDELIVERADDRESS));
        bean.setCity(rq.getCity());
        bean.setConsignee(rq.getConsignee());
        bean.setCountry(rq.getCountry());
        bean.setCreatetime(DateTools.getSystemTimeBi());
        bean.setDetail(rq.getDetail());
        if(StringUtils.isBlank(rq.getDistrict())){
            bean.setDistrict(StaticKey.EMPTY);
        }else{
            bean.setDistrict(rq.getDistrict());
        }
        bean.setEmail(rq.getEmail());
        bean.setIsdefault(rq.getIsdefault());
        bean.setLat(0.0);
        bean.setLng(0.0);
        bean.setMemberid(rq.getMemberid());
        bean.setMobile(rq.getMobile());
        bean.setPostcode(rq.getPostcode());
        bean.setProvince(rq.getProvince());
        bean.setSort(1);
        bean.setStatus(BaseEnums.DataUsingState.USING.getState());
        bean.setStreet(rq.getStreet());
        bean.setTag(rq.getTag());
        bean.setTelno(rq.getTelno());
        bean.setUpdatetime(DateTools.getSystemTimeBi());
        bean.setVersiont(1);
        this.memberDeliverAddressTService.save(bean);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

    @RequestMapping(value="/find",method= RequestMethod.POST)
    @Override
    public ResponseEntity find(@RequestBody MemberDeliverAddressT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            MemberDeliverAddressT bean=this.memberDeliverAddressTService.findByPK(MemberDeliverAddressT.class,rq.getId());
            if(bean!=null){
                responsedata.put(TagWords.BEAN,bean);
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
            }
        }
        return Response.success(responsedata);
    }

    @RequestMapping(value="/del",method= RequestMethod.POST)
    @Override
    public ResponseEntity del(@RequestBody MemberDeliverAddressT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }else{
            String ids[]=StringUtils.split(rq.getId(),TagWords.SPLITDOT);
            for(String s:ids){
                MemberDeliverAddressT bean=this.memberDeliverAddressTService.findByPK(MemberDeliverAddressT.class,s);
                if(bean!=null){
                    bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                    bean.setUpdatetime(DateTools.getSystemTimeBi());
                    this.memberDeliverAddressTService.update(bean);
                }
            }
            responsedata.put(TagWords.SUCCESS,true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    @RequestMapping(value="/update",method= RequestMethod.POST)
    @Override
    public ResponseEntity update(@RequestBody MemberDeliverAddressT rq) throws ParseException {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            MemberDeliverAddressT bean=this.memberDeliverAddressTService.findByPK(MemberDeliverAddressT.class,rq.getId());
            if(bean!=null){
                bean.setCity(rq.getCity());
                bean.setConsignee(rq.getConsignee());
                bean.setCountry(rq.getCountry());
                bean.setDetail(rq.getDetail());
                if(StringUtils.isBlank(rq.getDistrict())){
                    bean.setDistrict(StaticKey.EMPTY);
                }else{
                    bean.setDistrict(rq.getDistrict());
                }
                bean.setEmail(rq.getEmail());
                bean.setIsdefault(rq.getIsdefault());
                bean.setMobile(rq.getMobile());
                bean.setPostcode(rq.getPostcode());
                bean.setProvince(rq.getProvince());
                bean.setStreet(rq.getStreet());
                bean.setTag(rq.getTag());
                bean.setTelno(rq.getTelno());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                this.memberDeliverAddressTService.update(bean);
                responsedata.put(TagWords.SUCCESS, true);
                responsedata.put(TagWords.MSG, TagWords.OPERATION_SUCCESS);
            }
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }

    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        findCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        if(searchModel.getIsCustomSearch()==1){
            int currentPage=searchModel.getCalCurrentPage();
            int lineSize = searchModel.getLength();
            Criterion criterion= Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
            if(StringUtils.isNotBlank(search)){
                criterion=Restrictions.and(criterion).add(Restrictions.like("mobile",search,MatchMode.ANYWHERE));
            }
            int records=this.memberDeliverAddressTService.count(MemberDeliverAddressT.class,criterion).intValue();
            searchModel.setRecordsTotal(records);
            searchModel.setRecordsFiltered(records);
            responsedata.put(TagWords.RECORDSTOTAL,records);
            responsedata.put(TagWords.RECORDSFILTERED,records);
            List<MemberDeliverAddressT> list = this.memberDeliverAddressTService.findByCriteriaByPage(
                    MemberDeliverAddressT.class, criterion, Order.desc(TagWords.UPDATETIME), currentPage,
                    lineSize);
            this.processPageList(list);

        }else{
            if(StringUtils.isNotBlank(search)){
                filterSearch(searchModel,search);
            }else{
                findDefaultAll(searchModel,ordervalue,ordertype);
            }
        }
    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        criterion=Restrictions.and(criterion).add(Restrictions.like("mobile",search,MatchMode.ANYWHERE));
        int records=this.memberDeliverAddressTService.count(MemberDeliverAddressT.class,criterion).intValue();
        searchModel.setRecordsTotal(records);
        searchModel.setRecordsFiltered(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        List<MemberDeliverAddressT> list = this.memberDeliverAddressTService.findByCriteriaByPage(
                MemberDeliverAddressT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        int records=this.memberDeliverAddressTService.count(MemberDeliverAddressT.class,criterion).intValue();
        searchModel.setRecordsFiltered(records);
        searchModel.setRecordsTotal(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        Order order= BaseTools.orderByParam(TagWords.UPDATETIME,ordervalue,ordertype);
        List<MemberDeliverAddressT> list = this.memberDeliverAddressTService.findByCriteriaByPage(
                MemberDeliverAddressT.class, criterion,order, currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void processPageList(List<MemberDeliverAddressT> list) {
        List<Map<String,Object>>data=new ArrayList<>();
        for(MemberDeliverAddressT bean:list){
            Map<String,Object>cellMap=new HashMap<>();
            cellMap.put("id",bean.getId());
            cellMap.put("consignee",bean.getConsignee());
            cellMap.put("address",bean.getCountry()+bean.getProvince()+bean.getCity()+bean.getDistrict()+bean.getStreet()+bean.getDetail());
            cellMap.put("mobile",bean.getMobile());
            cellMap.put("tag", bean.getTag());
            cellMap.put("status", BaseEnums.DataUsingState.getName(bean.getStatus()));
            cellMap.put("updatetime",DateTools.formateLongDateToString(bean.getUpdatetime().longValue()));
            cellMap.put("version", bean.getVersiont());
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA,data);
    }


    /**
     * 过滤会员的收货地址
     * @param rq
     * @return
     */
    @RequestMapping(value="/filterMemberDeliverAddress",method= RequestMethod.POST)
    public ResponseEntity filterMemberDeliverAddress(MemberT rq) {
        Criterion criterion= Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        if(StringUtils.isNotBlank(rq.getMobile())){
            criterion=Restrictions.and(criterion).add(Restrictions.like("mobile",rq.getMobile(),MatchMode.ANYWHERE));
        }
        List<MemberDeliverAddressT> list = this.memberDeliverAddressTService.findByCriteria(MemberDeliverAddressT.class,criterion);
        responsedata.put(TagWords.BEANLIST,list);
        responsedata.put(TagWords.SUCCESS,true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }


    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
